package testjson;

import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Table;
import org.apache.hadoop.hbase.util.Bytes;
import org.junit.Test;
import untils.Hbaseutils;

import java.io.IOException;
import java.util.Random;

import static untils.Hbaseutils.getconnection;

public class Hbasemain {
    protected static final Log log = LogFactory.getLog(Hbasemain.class);
    public static Connection conns = null;
//    IMDto im=null;

    public Hbasemain(String zookeeperlist, String port) {
        conns = getconnection(zookeeperlist, port);


    }
     /*
     hbase 建表
     */

    public static boolean createImageScanTable(String table) {
        try {
            Admin admin = conns.getAdmin();
            if (!admin.tableExists(TableName.valueOf(table))) {
                HTableDescriptor tbdesc = new HTableDescriptor(TableName.valueOf(table));
                tbdesc.addFamily(new HColumnDescriptor("content"));
                admin.createTable(tbdesc);
            }

            admin.close();
            return true;
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        return false;
    }

    /*
    hbase插入数据
    */
    public static boolean insertImageScanRecord(String table, String rowid, String id, String name) {
        try {
            Put put = new Put(Bytes.toBytes(rowid));
            //  put.addColumn(Bytes.toBytes("content"), Bytes.toBytes("id"), Bytes.toBytes(pictureid));

            put.addColumn(Bytes.toBytes("content"), Bytes.toBytes("id"), Bytes.toBytes(id));
            put.addColumn(Bytes.toBytes("content"), Bytes.toBytes("name"), Bytes.toBytes(name));

            Table tb = conns.getTable(TableName.valueOf(table));
            tb.put(put);
            tb.close();
            return true;
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }

        return false;
    }


    /***
     * This method returns String rowkey
     * @param str The value to be rowkeys
     * @return rowkey string
     */
    public String rowkeys(String str) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < str.length(); i++) {
            buffer.append(str.charAt(new Random().nextInt(str.length())));

        }

        return buffer.toString();


    }


    @Test
    public void testJSON(String json) {
//        IMDto im=null;
        System.out.println(json);
        MaxwellDto dto = JSONObject.parseObject(json, MaxwellDto.class);
        String database=dto.getDatabase();
        String tablename = dto.getTable();
        System.out.println("dto:"+dto);
        log.info("tablename" + tablename);
        log.info("type" + dto.getType());



//        try {
//            im = JSONObject.parseObject(dto.getData(), IMDto.class);
//            System.out.println(im);
//        } catch (Exception e1) {
//            throw e1;
//
//        }

//if(im!=null&&!im.equals(""))
//&&im.getId()!=null&&im.getName()!=null
        if (dto.getDatabase().equals("test_maxwell")&&dto.getType().equals("insert")) {
            IMDto im = JSONObject.parseObject(dto.getData(), IMDto.class);


//                try {
//                    im = JSONObject.parseObject(dto.getData(), IMDto.class);
//
//                } catch (Exception e1) {
//                    throw e1;
//
//                }
                 System.out.println("wwwwwwwwwwwwwww"+im);




            if (!createImageScanTable(tablename)) {
                System.out.println("check or create image scan table(" + tablename + ") error  ");
                return;
            }

            //  String  rowid="111111";

            String rowid = rowkeys("abcdefg012345");
            String id;
            String name;
            id = im.getId();
            name = im.getName();
            if(id==null)
                id="null";
            if(name==null)
                name="null";


            if (!insertImageScanRecord(tablename, rowid, id, new String(name))) {
                System.out.println("insert image scan record errortable: " + tablename);
                return;
            }
          //  System.out.println(id + name + " is imported");
            System.out.println(id+ name + " is imported");


        }


    }


}
